package com.example.comment.controller;

import cn.hutool.core.lang.Assert;
import com.alibaba.fastjson.JSON;
import com.example.comment.base.dto.CommentAddDTO;
import com.example.comment.base.dto.CommentQueryDTO;
import com.example.comment.base.dto.CommentRemoveDTO;
import com.example.comment.base.dto.LikeDTO;
import com.example.comment.base.vo.CommentVO;
import com.example.comment.mq.config.CommentMqProperties;
import com.example.comment.service.CommentLikeService;
import com.example.comment.service.CommentService;
import com.example.comment.utils.PageResult;
import com.example.comment.utils.R;
import com.example.comment.utils.SystemException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author QingZeyu
 * @date 2024/5/7 14:25
 */
@RequestMapping("/comment")
@RestController
@Api(value = "评论管理", tags = "评论管理")
public class CommentController {
    @Resource
    private CommentService commentService;
    @Resource
    private CommentLikeService commentLikeService;

    @Resource
    private RabbitTemplate rabbitTemplate;

    @Resource
    private CommentMqProperties commentMqProperties;

    @GetMapping("/page")
    @ApiOperation(value = "分页查询评论")
    public R<PageResult<CommentVO>> page(CommentQueryDTO dto) {
        Assert.isTrue(Objects.nonNull(dto.getReferenceId()),() -> new SystemException("关联Id不能为空"));
        Assert.isTrue(Objects.nonNull(dto.getUserId()),() -> new SystemException("当前用户Id不能为空"));
        return R.ok(commentService.page(dto));
    }

    @PutMapping("/like")
    @ApiOperation(value = "点赞/取消点赞")
    public R<Void> like(@RequestBody @Valid LikeDTO dto){
        rabbitTemplate.convertAndSend(commentMqProperties.getExchange(),commentMqProperties.getRoutingKey(), JSON.toJSONString(dto));
        return R.ok();
    }

    @PutMapping("/add")
    @ApiOperation(value = "添加评论")
    public R<Boolean> add(@RequestBody @Valid CommentAddDTO dto){
        return R.ok(commentService.addComment(dto));
    }

    @DeleteMapping("/remove")
    @ApiOperation(value = "删除评论")
    public R<Boolean> remove(@RequestBody @Valid CommentRemoveDTO dto){
        return R.ok(commentService.removeComment(dto.getUserId(), dto.getCommentId()));
    }
}
